#import "YYText.h"
#import "GKCover.h"
#import "dfhgwhruebvhrheymournrural.h"
#import "dfhgwhruebvhrheyspotfaint.h"
#import "dfhgwhruebvhrheyluggagerecorder.h"
#import "dfhgwhruebvhrheyoutwarddelegate.h"
#import "dfhgwhruebvhrheyfreightencourage.h"
#import "dfhgwhruebvhrheydelegatelimp.h"
#import "dfhgwhruebvhrheydairyinvest.h"
#import "dfhgwhruebvhrheyultimateintrinsic.h"
#import "dfhgwhruebvhrheycountermanner.h"
#import "dfhgwhruebvhrheyvacantbound.h"
#import "dfhgwhruebvhrheyslaughtertransmission.h"
#import "dfhgwhruebvhrheymunicipalautomobile.h"
#import "dfhgwhruebvhrheytragedymotivateViewController.h"
#import "dfhgwhruebvhrheyroomcatalogViewController.h"
#import "dfhgwhruebvhrheydizzycurveViewController.h"
@interface dfhgwhruebvhrheydizzycurveViewController ()
@property (nonatomic, strong) dfhgwhruebvhrheymournrural *timer;
@property (nonatomic, copy) NSString *password;
@property (nonatomic, assign) BOOL autoSendSms;
@property (nonatomic, assign) dhgfywegwygeydwregardlessfolk onLineStyle;
@property (nonatomic, strong) dfhgwhruebvhrheyslaughtertransmission *nameItemView;
@property (nonatomic, strong) dfhgwhruebvhrheyslaughtertransmission *accountNoItemView;
@property (nonatomic, strong) dfhgwhruebvhrheyultimateintrinsic *defaultBankModel;
@property (nonatomic, strong) PPP_AccountItemModel *defaultEwalltModel;
@property (nonatomic, strong) dfhgwhruebvhrheycountermanner *checkDevicePopView;
@end
@implementation dfhgwhruebvhrheydizzycurveViewController {
	int cutdown;
	UIButton *loanBtn;
	YYLabel *tipsLabel;
}
- (instancetype)initWithStyle:(dhgfywegwygeydwregardlessfolk)style {
	if([super init]) {
		_onLineStyle = style;
	}
	return self;
}
- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden) name:UIKeyboardWillHideNotification object:nil];
	[self loadAccountInfo];
}
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
	[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
	[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow {
	[UIView animateWithDuration:0.25 animations:^{
		dfhgwhruebvhrheyoptionsaving.keyWindow.php_y = -60;
	}];
}
- (void)keyboardWillHidden {
	[UIView animateWithDuration:0.25 animations:^{
		dfhgwhruebvhrheyoptionsaving.keyWindow.php_y = 0;
	}];
}
- (void)viewDidLoad {
    [super viewDidLoad];
	[self loadUI];
}
- (void)loadUI {
	self.autoSendSms = YES;
	self.backgroundColor = [UIColor whiteColor];
	self.navigationStyle = dhgfywegwygeydwreciprocaltransport;
	__weak typeof(self) weakSelf = self;
	self.nameItemView = [[NSBundle mainBundle] loadNibNamed:@"dfhgwhruebvhrheyslaughtertransmission" owner:nil options:nil].firstObject;
	self.nameItemView.nameTextLabel.text = self.onLineStyle == dhgfywegwygeydwexaggeratesemester ? @"E-Wallet Name" : @"Bank Name";
	[self.containerView addSubview:self.nameItemView];
	self.nameItemView.inputView.type = 1;
	[self.nameItemView.inputView inputTextViewClick:^(dfhgwhruebvhrheysecurityinjure * _Nonnull clickView) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		[strongSelf accountList];
	}];
	self.accountNoItemView = [[NSBundle mainBundle] loadNibNamed:@"dfhgwhruebvhrheyslaughtertransmission" owner:nil options:nil].firstObject;
	self.accountNoItemView.nameTextLabel.text = self.onLineStyle == dhgfywegwygeydwexaggeratesemester ? @"E-Wallet Account No." : @"Bank Account No.";
	[self.containerView addSubview:self.accountNoItemView];
	self.accountNoItemView.more.hidden = YES;
	self.accountNoItemView.inputView.type = 1;
	self.accountNoItemView.inputView.keyboardType = UIKeyboardTypeNumberPad;
	[self.accountNoItemView.inputView inputTextViewClick:^(dfhgwhruebvhrheysecurityinjure * _Nonnull clickView) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		[strongSelf accountList];
	}];
	NSString *tipsString = @"1. Please note that the withdrawal method will not be change after select it. \n2. Other unofficial repayment channels are not acceptable to avoid being frauded. \n3. If you pay the loan on time,you will enjoy the audit exemption period.If you are several days overdue,your next loan limit may reduced to the minimum.";
	NSMutableAttributedString *tipsAttribute = [[NSMutableAttributedString alloc] initWithString:tipsString];
	NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
	style.lineSpacing = 5;
	tipsAttribute.yy_paragraphStyle = style;
	tipsAttribute.yy_font = [UIFont regular:14];
	tipsAttribute.yy_color = @"#AAAAAA".dhgfywegwygeydwlearneddesignate;
	tipsLabel = [[YYLabel alloc] init];
	tipsLabel.numberOfLines = 0;
	[self.containerView addSubview:tipsLabel];
	tipsLabel.attributedText = tipsAttribute;
	loanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
	loanBtn.titleLabel.font = [UIFont semiBold:18];
	loanBtn.layer.cornerRadius = 10;
	loanBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
	[loanBtn setTitle:@"Get Loan" forState:UIControlStateNormal];
	[loanBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	[loanBtn addTarget:self action:@selector(loanBtnBtnAction) forControlEvents:UIControlEventTouchUpInside];
	[self.containerView addSubview:loanBtn];
}
- (void)loadAccountInfo {
	switch (self.onLineStyle) {
		case dhgfywegwygeydwsemesterfurious: {
			[self defaultBankCard];
		}
			break;
		case dhgfywegwygeydwexaggeratesemester: {
			[self getUsewEwalletList];
		}
			break;
		default:
			break;
	}
}
- (void)defaultBankCard {
	[dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
	__weak typeof(self) weakSelf = self;
	[[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwlambbreast path:getDefaultBankApi isEncrypt:YES withParams:nil success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
		__strong typeof(weakSelf) strongSelf = weakSelf;
		strongSelf.defaultBankModel = [dfhgwhruebvhrheyultimateintrinsic yy_modelWithJSON:json];
		if (strongSelf.defaultBankModel.code == 200) {
			[strongSelf.nameItemView.inputView bindContent:strongSelf.defaultBankModel.bank_name];
			[strongSelf.accountNoItemView.inputView bindContent:strongSelf.defaultBankModel.bank_number];
		}else {
			[dfhgwhruebvhrheyoptionsaving.keyWindow show:strongSelf.defaultBankModel.error];
		}
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
	}];
}
- (void)getUsewEwalletList {
	[dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
	__weak typeof(self) weakSelf = self;
	[[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwlambbreast path:ewalletListApi isEncrypt:YES withParams:nil success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
		__strong typeof(weakSelf) strongSelf = weakSelf;
		dfhgwhruebvhrheyfreightencourage *model = [dfhgwhruebvhrheyfreightencourage yy_modelWithJSON:json];
		if (model.code == 200) {
			for (PPP_AccountItemModel *itemModel in model.data) {
				if ([itemModel.is_default isEqualToString:@"1"]) {
					strongSelf.defaultEwalltModel = itemModel;
					[strongSelf.nameItemView.inputView bindContent:itemModel.title];
					[strongSelf.accountNoItemView.inputView bindContent:itemModel.account_number];
					break;
				}
			}
		}else {
			[dfhgwhruebvhrheyoptionsaving.keyWindow show:model.error];
		}
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
	}];
}
- (void)accountList {
	__weak typeof(self) weakSelf = self;
	dfhgwhruebvhrheyterritoryexaggerateViewController *list = [[dfhgwhruebvhrheyterritoryexaggerateViewController alloc] initWithStyle:self.onLineStyle];
	[self.navigationController pushViewController:list animated:YES];
	[list bindDefaultAccount:^(BOOL ret) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		[strongSelf loadAccountInfo];
	}];
}
- (void)startTimer {
	__weak typeof(self) weakSelf = self;
	[self.timer startWithAction:^{
		__strong typeof(weakSelf) strongSelf = weakSelf;
		dispatch_async(dispatch_get_main_queue(), ^{
			[strongSelf updateTimer];
		});
	}];
}
- (void)updateTimer {
	if (cutdown <= 0) {
		cutdown = PPP_Code_Cut_Time;
		if (_timer) {
			[_timer cancel];
			_timer = nil;
		}
		self.checkDevicePopView.sendBtn.userInteractionEnabled = YES;
		self.checkDevicePopView.sendBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
		[self.checkDevicePopView.sendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
		[self.checkDevicePopView.sendBtn setTitle:@"Send" forState:UIControlStateNormal];
	}else {
		self.checkDevicePopView.sendBtn.userInteractionEnabled = NO;
		self.checkDevicePopView.sendBtn.backgroundColor = @"#EEEEEE".dhgfywegwygeydwlearneddesignate;
		[self.checkDevicePopView.sendBtn setTitleColor:@"#AAAAAA".dhgfywegwygeydwlearneddesignate forState:UIControlStateNormal];
		[self.checkDevicePopView.sendBtn setTitle:[NSString stringWithFormat:@"%02ds", cutdown] forState:UIControlStateNormal];
		cutdown -= 1;
	}
}
- (void)loanSuccess {
	__weak typeof(self) weakSelf = self;
	dfhgwhruebvhrheymunicipalautomobile *popView = [[NSBundle mainBundle] loadNibNamed:@"dfhgwhruebvhrheymunicipalautomobile" owner:nil options:nil].firstObject;
	popView.frame = CGRectMake(13, 0, dhgfywegwygeydwdispositionvisualize-13*2, 352);
	[GKCover coverFrom:[dfhgwhruebvhrheyoptionsaving keyWindow] contentView:popView style:GKCoverStyleTranslucent showStyle:GKCoverShowStyleCenter showAnimStyle:GKCoverShowAnimStyleCenter hideAnimStyle:GKCoverHideAnimStyleCenter notClick:YES];
	[popView btnClick:^{
		__strong typeof(weakSelf) strongSelf = weakSelf;
		[GKCover hideCover];
		[strongSelf.navigationController popToRootViewControllerAnimated:YES];
	}];
}
- (void)sendSms {
	NSMutableDictionary *params = [NSMutableDictionary dictionary];
	params[@"tel_number"] = [dfhgwhruebvhrheyoptionsaving getUserData].phone;
	[dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
	__weak typeof(self) weakSelf = self;
	[[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwawaitlamb path:sendLoanSmsApi isEncrypt:YES withParams:params success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
		dfhgwhruebvhrheysternideology *model = [dfhgwhruebvhrheysternideology yy_modelWithJSON:json];
		if (model.code == 200) {
			[strongSelf startTimer];
		}else {
			[dfhgwhruebvhrheyoptionsaving.keyWindow show:model.error];
		}
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
	}];
}
- (void)showLoanSmsPopView {
	__weak typeof(self) weakSelf = self;
	if (self.autoSendSms) {
		[self sendSms];
	}
	self.autoSendSms = NO;
	cutdown = PPP_Code_Cut_Time;
	self.checkDevicePopView = [[NSBundle mainBundle] loadNibNamed:@"dfhgwhruebvhrheycountermanner" owner:nil options:nil].firstObject;
	self.checkDevicePopView.frame = CGRectMake(13, 0, dhgfywegwygeydwdispositionvisualize-13*2, 348);
	[GKCover coverFrom:[dfhgwhruebvhrheyoptionsaving keyWindow] contentView:self.checkDevicePopView style:GKCoverStyleTranslucent showStyle:GKCoverShowStyleCenter showAnimStyle:GKCoverShowAnimStyleCenter hideAnimStyle:GKCoverHideAnimStyleCenter notClick:YES];
	[self.checkDevicePopView sureViewBtnsClick:^(UIButton * _Nonnull sender, NSString * _Nonnull code) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		if (sender.tag == 100) {
			[strongSelf sendSms];
		}else {
			[GKCover hideCover];
			[strongSelf getMoney:code];
		}
	}];
}
- (void)checkLoanDevice {
	NSLog(@"检测是否需要发送验证码");
	if (self.password.length != 6) {
		return [dfhgwhruebvhrheyoptionsaving.keyWindow show:@"Please enter the complete password."];
	}
	[dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
	__weak typeof(self) weakSelf = self;
	[[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwawaitlamb path:checkLoanDeviceApi isEncrypt:YES withParams:nil success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
		__strong typeof(weakSelf) strongSelf = weakSelf;
		dfhgwhruebvhrheydelegatelimp *model = [dfhgwhruebvhrheydelegatelimp yy_modelWithJSON:json];
		if (model.code == 200) {
			if (model.code_type == 0) {
				[strongSelf showLoanSmsPopView];
			}else {
				[strongSelf getMoney:nil];
			}
		}else {
			[dfhgwhruebvhrheyoptionsaving.keyWindow show:model.error];
		}
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
	}];
}
- (void)getMoney:(nullable NSString *)code {
	NSString *apiPath;
	NSMutableDictionary *params = [NSMutableDictionary dictionary];
	params[@"price"] = self.selectedAmount.amount;
	params[@"rateid"] = self.selectedAmount.rid;
	params[@"tid"] = self.seletedTerm.ud_id;
	params[@"days"] = self.seletedTerm.days;
	params[@"user_passwords"] = self.password;
	params[@"riskdata"] = [dfhgwhruebvhrheyoptionsaving getRiskJsonData];
	params[@"sms_code"] = code;
	params[@"pro_id"] = self.seletedTerm.info.p_id;
	params[@"dev_id"] = [dfhgwhruebvhrheyoptionsaving uuid].dhgfywegwygeydwscholarshiptexture;
	switch (self.onLineStyle) {
		case dhgfywegwygeydwsemesterfurious: {
			apiPath = loanWithBankApi;
		}
			break;
		case dhgfywegwygeydwexaggeratesemester: {
			apiPath = loanWithEwalletApi;
			params[@"wc_id"] = self.defaultEwalltModel.wa_id;
			params[@"acu_number"] = self.defaultEwalltModel.account_number;
		}
			break;
		default:
			break;
	}
	__weak typeof(self) weakSelf = self;
	[dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
	[[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwawaitlamb path:apiPath isEncrypt:YES withParams:params success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
		dfhgwhruebvhrheydairyinvest *model = [dfhgwhruebvhrheydairyinvest yy_modelWithJSON:json];
		if (model.code == 200) {
			[strongSelf loanSuccess];
			[dfhgwhruebvhrheyoptionsaving uploadRiskDataByCatchId:@"2" andApplyId:model.oid withDetailId:model.deta_id];
		}else {
			[dfhgwhruebvhrheyoptionsaving.keyWindow show:model.error];
		}
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
	}];
}
- (void)loanBtnBtnAction {
	__weak typeof(self) weakSelf = self;
	dfhgwhruebvhrheyvacantbound *popView = [[NSBundle mainBundle] loadNibNamed:@"dfhgwhruebvhrheyvacantbound" owner:nil options:nil].firstObject;
	popView.frame = CGRectMake(13, 0, dhgfywegwygeydwdispositionvisualize-13*2, 489);
	popView.type = self.onLineStyle == dhgfywegwygeydwexaggeratesemester ? 1 : 2;
	popView.defaultBankModel = self.defaultBankModel;
	popView.defaultEwalltModel = self.defaultEwalltModel;
	[GKCover coverFrom:[dfhgwhruebvhrheyoptionsaving keyWindow] contentView:popView style:GKCoverStyleTranslucent showStyle:GKCoverShowStyleCenter showAnimStyle:GKCoverShowAnimStyleCenter hideAnimStyle:GKCoverHideAnimStyleCenter notClick:YES];
	[popView bindData:self.seletedTerm andAmount:self.selectedAmount withBtnsClick:^(UIButton * _Nonnull sender, NSString * _Nonnull code) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		[GKCover hideCover];
		if (sender.tag == 101) {
			strongSelf.password = code;
			[strongSelf checkLoanDevice];
		}
		if (sender.tag == 200) {
            NSLog(@"取款忘记密码");
			dfhgwhruebvhrheytragedymotivateViewController *forgetVC = [[dfhgwhruebvhrheytragedymotivateViewController alloc] init];
            forgetVC.mobile = [dfhgwhruebvhrheyoptionsaving getUserData].phone;
            forgetVC.canNotEditMobile = YES;
            [self.navigationController pushViewController:forgetVC animated:YES];
		}
	}];
}
- (void)viewDidLayoutSubviews {
	[super viewDidLayoutSubviews];
	[self.nameItemView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.centerX.equalTo(self.containerView);
		make.top.equalTo(self.containerView).offset(20);
	}];
	[self.accountNoItemView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(self.nameItemView.mas_bottom).offset(16);
		make.left.centerX.equalTo(self.nameItemView);
	}];
	[tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(self.accountNoItemView.mas_bottom).offset(15);
		make.left.equalTo(self.containerView).offset(13);
		make.centerX.equalTo(self.containerView);
		make.height.mas_equalTo(160);
	}];
	[loanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(tipsLabel.mas_bottom).offset(50);
		make.left.centerX.equalTo(tipsLabel);
		make.height.mas_equalTo(50);
		make.bottom.equalTo(self.containerView).offset(-15);
	}];
}
@end
